<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    table {
      border-collapse: collapse;
    }
  </style>
</head>

<body>
  <table id="myTable" border>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
      <td>D</td>
    </tr>
    <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
      <td>D</td>
    </tr>
    <tr>
      <td>E</td>
      <td>F</td>
      <td>G</td>
      <td>H</td>
    </tr>
  </table>

  <script>
    // 获取表格对象
    var table = document.getElementById("myTable");
    // 定义一个数组来存储分组信息
    var groups = [];

    // 遍历每一行中的单元格
    for (var i = 0; i < table.rows.length; i++) {
      var cells = table.rows[i].cells;
      for (var j = 0; j < cells.length; j++) {
        var text = cells[j].innerHTML;
        // 查找是否与前面的单元格相同
        var groupIndex = -1;
        for (var k = 0; k < groups.length; k++) {
          var group = groups[k];
          if (group.endRow === i && group.endCol === j - 1 && group.text === text) {
            group.endCol = j;
            group.colspan += 1;
            groupIndex = k;
            break;
          }
        }
        // 如果没有找到，则创建新的分组
        if (groupIndex === -1) {
          var group = {
            startRow: i,
            startCol: j,
            endRow: i,
            endCol: j,
            rowspan: 1,
            colspan: 1,
            text: text
          };
          groups.push(group);
        }
      }
    }

    // 遍历每一列中的单元格
    for (var j = 0; j < table.rows[0].cells.length; j++) {
      for (var i = 0; i < table.rows.length; i++) {
        var text = table.rows[i].cells[j].innerHTML;
        // 查找是否与上面的单元格相同
        var groupIndex = -1;
        for (var k = 0; k < groups.length; k++) {
          var group = groups[k];
          if (group.endRow === i - 1 && group.endCol === j && group.text === text) {
            group.endRow = i;
            group.rowspan += 1;
            groupIndex = k;
            break;
          }
        }
        // 如果没有找到，则不需要合并
      }
    }

    console.log(groups)
  </script>
</body>
</html>